package com.xuanyuanxinyu.func.executor

import io.reactivex.schedulers.Schedulers
import java.util.concurrent.BlockingQueue
import java.util.concurrent.Executors
import java.util.concurrent.LinkedBlockingQueue
import java.util.concurrent.ThreadPoolExecutor
import java.util.concurrent.TimeUnit

/**
 * @author ken.luo
 * @version V1.0
 * @FileName:
 * @Description:
 * @date 2023/4/4
 * @email 424533553@qq.com
 */
object ExecutorsFactory {
    
    private var playerThreadPool : ThreadPoolExecutor? = null
    private var downloadThreadPool : ThreadPoolExecutor? = null
    private fun createExecutor() {
        val queue : BlockingQueue<Runnable> = LinkedBlockingQueue(100)
        val corePoolSize = 1 // 核心线程数
        val maximumPoolSize = 1 // 最大线程数
        val keepAliveTime = 30 // 非核心线程的空闲存活时长（分钟）
        val queueCapacity = 9999 // 队列最大长度
        playerThreadPool = ThreadPoolExecutor(corePoolSize,maximumPoolSize,keepAliveTime.toLong(),TimeUnit.MINUTES,queue)
        downloadThreadPool = ThreadPoolExecutor(corePoolSize,maximumPoolSize,keepAliveTime.toLong(),TimeUnit.MINUTES,queue)
        //        return new ThreadPoolExecutor(5, 10,
        //                0L, TimeUnit.MILLISECONDS,
        //                new LinkedBlockingQueue<>(),
        //                new PictureThreadUtils.UtilsThreadFactory("single", priority)
    }
    
    /**
     * 执行任务
     */
    fun executePlayer(runnable : Runnable){
        if(playerThreadPool ==null){
            createExecutor()
        }
    
        playerThreadPool?.apply {
            if (!isShutdown) {
                execute(runnable)
            }
        }
    }
    
    /**
     * 执行任务
     */
    fun executeDown(runnable : Runnable){
        if(downloadThreadPool ==null){
            createExecutor()
        }
    
        downloadThreadPool?.apply {
            if (!isShutdown) {
                execute(runnable)
            }
        }
    }
    
    
    /**
     * 创建自定义的任务
     */
    private fun creatSelfScheduler() {
        val executor = Executors.newFixedThreadPool(10)
        val pooledScheduler = Schedulers.from(executor)
    }
    
}